<?php
 include 'theme_options.php';
 include 'guide.php';

if ( function_exists('register_sidebar') )
    register_sidebar(array(
	'name' => 'Sidebar1',
    'before_widget' => '<li class="sidebox" id="%1$s">',
    'after_widget' => '</li>',
	'before_title' => '<h3 class="sidetitl">',
    'after_title' => '</h3>',
    ));

    register_sidebar(array(
	'name' => 'Sidebar2',
    'before_widget' => '<li class="sidebox" id="%1$s">',
    'after_widget' => '</li>',
	'before_title' => '<h3 class="sidetitl">',
    'after_title' => '</h3>',
    ));	

	register_sidebar(array(
	'name' => 'Footer',
    'before_widget' => '<li class="botwid" id="%1$s">',
    'after_widget' => '</li>',
	'before_title' => '<h3 class="bothead">',
    'after_title' => '</h3>',
    ));		
	
register_nav_menus( array(
		'primary' => __( 'Primary Navigation', '' ),
		'secondary' => __( 'Secondary Navigation', '' ),
	) );
	
	
if ( function_exists( 'add_theme_support' ) ) { // Added in 2.9
	add_theme_support( 'post-thumbnails' );
	add_image_size( 'quest-slider', 630, 280, true );
	add_image_size( 'quest-home', 250, 100, true ); 
	add_image_size( 'quest-box', 80, 60, true ); 
}

function slidim(){
if ( has_post_thumbnail() ) {
			 the_post_thumbnail( 'quest-slider', array('class' => 'slidim ') );
} else {
}
}

function boxim(){
if ( has_post_thumbnail() ) {
			 the_post_thumbnail( 'quest-box', array('class' => 'boximage') );
} else {
?>
<img class="boximage" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" />
<?php
}
}

function postim(){
if ( has_post_thumbnail() ) {
			 the_post_thumbnail( 'quest-home', array('class' => 'postim') );
} else {
}
}

function new_excerpt_length($length) {
	return 80;
}
add_filter('excerpt_length', 'new_excerpt_length');

function ShortenText($text)
{
$chars_limit = 30;
$chars_text = strlen($text);
$text = $text." ";
$text = substr($text,0,$chars_limit);
$text = substr($text,0,strrpos($text,' '));
if ($chars_text > $chars_limit)
{
$text = $text."...";
}
return $text;
}
				
function seo_wp_title( $title, $separator ) {
	if ( is_feed() )
	return $title;
	global $paged, $page;
	if ( is_search() ) {
		$title = sprintf( __( 'Search results for %s', '' ), '"' . get_search_query() . '"' );
		if ( $paged >= 2 )
			$title .= " $separator " . sprintf( __( 'Page %s', '' ), $paged );
		$title .= " $separator " . get_bloginfo( 'name', 'display' );
		return $title;
	}
	$title .= get_bloginfo( 'name', 'display' );
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title .= " $separator " . $site_description;
	if ( $paged >= 2 || $page >= 2 )
		$title .= " $separator " . sprintf( __( 'Page %s', '' ), max( $paged, $page ) );
	return $title;
}			
	

add_filter( 'wp_title', 'seo_wp_title', 10, 2 );

//related post shortcode
function related_posts_shortcode( $atts ) {
 
	extract(shortcode_atts(array(
	    'limit' => '5',
	), $atts));
 
	global $wpdb, $post, $table_prefix;
 
	if ($post->ID) {
 
		$retval = '
<ul>';
 
		// Get tags
		$tags = wp_get_post_tags($post->ID);
		$tagsarray = array();
		foreach ($tags as $tag) {
			$tagsarray[] = $tag->term_id;
		}
		$tagslist = implode(',', $tagsarray);
 
		// Do the query
		$q = "
			SELECT p.*, count(tr.object_id) as count
			FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p
			WHERE tt.taxonomy ='post_tag'
				AND tt.term_taxonomy_id = tr.term_taxonomy_id
				AND tr.object_id  = p.ID
				AND tt.term_id IN ($tagslist)
				AND p.ID != $post->ID
				AND p.post_status = 'publish'
				AND p.post_date_gmt < NOW()
			GROUP BY tr.object_id
			ORDER BY count DESC, p.post_date_gmt DESC
			LIMIT $limit;";
 
		$related = $wpdb->get_results($q);
 
		if ( $related ) {
			foreach($related as $r) {
				$retval .= '
	<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>
';
			}
		} else {
			$retval .= '
	<li>No related posts found</li>
';
		}
		$retval .= '</ul>
';
		return $retval;
	}
	return;
}
add_shortcode('related_posts', 'related_posts_shortcode');

?>